import time
from pathlib import Path

from ScrapyObject.spiders.utils.bc import batch_tga_to_png


def list_files_with_pathlib(folder_path):
    try:
        folder = Path(folder_path)
        if not folder.exists():
            print(f"错误：目录 '{folder_path}' 不存在。")
            return []
        if not folder.is_dir():
            print(f"错误：'{folder_path}' 不是一个目录。")
            return []

        # 获取所有文件（不包括子目录）
        files = [file.name for file in folder.iterdir() if file.is_file()]
        return files
    except PermissionError:
        print(f"错误：没有权限访问目录 '{folder_path}'。")
        return []


def get_subfolders(path):
    p = Path(path)
    # 只获取第一层子文件夹
    return [f for f in p.iterdir() if f.is_dir()]


if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    folder_path = "D:\\2\\"
    folders = get_subfolders(folder_path)
    for index, f in enumerate(folders):
        if index % 1000 == 0:
            print(str(index) + "   " + str(len(folders)))
        folder_2 = str(f).replace('\\2\\', '\\3\\')
        batch_tga_to_png(str(f), folder_2)
    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
